package com.dongrj.framework.log4jdbc;


import com.dongrj.framework.core.constant.Constant;
import com.dongrj.framework.core.utils.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * 可以使用该后置拦截器实现的功能
 * 1、配置属性的加解密
 * 2、删除配置文件的属性值
 * 3、添加属性值
 */
@Order
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {


    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        if (isProd(environment)) {
            System.out.println("当前是生产环境跳过自定义配置!");
            return;
        }
        Map<String, Object> properties = new HashMap<>(5);
        properties.put("logging.level.jdbc.audit ", "ERROR");
        properties.put("logging.level.jdbc.sqltiming", "ERROR");
        properties.put("logging.level.jdbc.resultset", "ERROR");
        properties.put("logging.level.jdbc.resultsettable", "ERROR");
        properties.put("logging.level.jdbc.connection", "ERROR");
        environment.getPropertySources().addFirst(new MapPropertySource("log4jdbc-close", properties));
    }

    private boolean isProd(ConfigurableEnvironment environment) {
        String property = environment.getProperty("spring.profiles.active");
        return StringUtils.equals(property, Constant.ENv.PROD);
    }
}

